using System;
using System.Globalization;

using LambdaGp.Core;

namespace LambdaGp.Operators.Terminals
{
    public class EphemeralInstance : Terminal, IParameterMutatable
    {
        private readonly Ephemeral _ephemeral;

        public EphemeralInstance(Ephemeral ephemeral, double value)
            : base(value.ToString(CultureInfo.InvariantCulture).Replace(".", ","))
        {
            Value = value;
            _ephemeral = ephemeral;
        }

        public override bool RequiresParameterMutation { get { return true; } }

        protected double Value { get; private set; }

        public override Func<double> CompileToFunc(Individual individual, OperatorNode operatorNode)
        {
            return () => Value;
        }

        public virtual Operator ParameterMutate()
        {
            double newValue = _ephemeral.SlightlyMutateValue(Value);
            EphemeralInstance newEphemeral = _ephemeral.CreateInstance(newValue);
            return newEphemeral;
        }
    }
}
